package com.ttg.web.dao.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.common.enums.GradeTypeEnum;
import com.ttg.model.dto.MemberConfigListDto;
import com.ttg.model.mapper.VipGradeConfigMapper;
import com.ttg.model.pojo.VipGradeConfig;
import com.ttg.web.dao.VipGradeConfigDao;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;

/**
 * @author zhangHongYuan
 * @description
 * @date 2023/6/8 11:20
 */
@Repository
public class VipGradeConfigDaoImpl extends ServiceImpl<VipGradeConfigMapper, VipGradeConfig> implements VipGradeConfigDao {

    @Override
    public List<VipGradeConfig> getMemberConfigList(MemberConfigListDto dto) {
        LambdaQueryWrapper<VipGradeConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VipGradeConfig::getGradeType,dto.getGradeType());
        return list(wrapper);
    }

    @Override
    public VipGradeConfig getMemberConfigByGrade(String grade) {
        LambdaQueryWrapper<VipGradeConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VipGradeConfig::getVipGrade,grade);
        return getOne(wrapper);
    }

    @Override
    public VipGradeConfig queryVipGradeDetail(Double vipGrade,Integer gradeType) {
        return getOne(Wrappers.<VipGradeConfig>lambdaQuery().eq(VipGradeConfig::getVipGrade, vipGrade).eq(VipGradeConfig::getGradeType,gradeType));
    }

    @Override
    public List<VipGradeConfig> getVipGradeNameList(Integer gradeType) {
        LambdaQueryWrapper<VipGradeConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(VipGradeConfig::getVipName,VipGradeConfig::getVipGrade);
        wrapper.eq(VipGradeConfig::getGradeType,gradeType);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<VipGradeConfig> getVipGradeByType(Integer gradeType) {
        LambdaQueryWrapper<VipGradeConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VipGradeConfig::getGradeType,gradeType);
        return list(wrapper);
    }

    @Override
    public List<VipGradeConfig> getVipGradeByTypeForEdit(Integer gradeType) {
        LambdaQueryWrapper<VipGradeConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(VipGradeConfig::getId,VipGradeConfig::getVipGrade,VipGradeConfig::getGradeType)
                .eq(VipGradeConfig::getGradeType,gradeType);
        return list(wrapper);
    }

    @Override
    public void sumBindTags(Set<Integer> configIds) {
        baseMapper.sumBindTags(configIds);
    }


    @Override
    public String getVipNewGradeByVipId(String id) {
        String vipNewGradeByVipId = baseMapper.getVipNewGradeByVipId(id);
        if (vipNewGradeByVipId != null) {
            return vipNewGradeByVipId;
        }
        return getOne(new LambdaQueryWrapper<VipGradeConfig>()
                .select(VipGradeConfig::getVipGrade)
                .eq(VipGradeConfig::getGradeType, GradeTypeEnum.TYPE_PRIVATE.getValue())
                .last("LIMIT 1")).getVipGrade().toString();

    }

    @Override
    public String getMctNewGradeByVipId(String id) {
        String mctNewGradeByVipId = baseMapper.getMctNewGradeByVipId(id);
        if (mctNewGradeByVipId!=null){
            return mctNewGradeByVipId;
        }
        return getOne(new LambdaQueryWrapper<VipGradeConfig>()
                .select(VipGradeConfig::getVipGrade)
                .eq(VipGradeConfig::getGradeType, GradeTypeEnum.TYPE_PUBLIC.getValue())
                .last("LIMIT 1")).getVipGrade().toString();
    }
}
